package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import com.tanhua.service.CommentApi;
import com.tanhua.service.MovementApi;
import com.tanhua.service.UserInfoApi;
import com.tanhua.service.VideoApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author げっしょく
 * @date 2021/11/3
 */
@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @DubboReference
    private CommentApi commentApi;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public PageResult getUsers(Integer page, Integer pagesize) {
        //分页获取用户
        PageResult pageResult = userInfoApi.getUserPageUser(page, pagesize);
        List<UserInfo> items = (List<UserInfo>) pageResult.getItems();
        List<UserInfo> list = new ArrayList<>();
        for (UserInfo item : items) {
            String key = Constants.USER_FREEZE + item.getId();
            if (redisTemplate.hasKey(key)) {
                item.setUserStatus("2");
            }
            list.add(item);
        }
        pageResult.setItems(list);
        //封装返回类
        return pageResult;
    }

    public PageResult getVideos(Integer page, Integer pagesize, Long userId) {
        PageResult pageResult = videoApi.getByIdVideos(userId, page, pagesize);
        List<Video> items = (List<Video>) pageResult.getItems();
        if (CollUtil.isEmpty(items)) {
            return pageResult;
        }
        List<Long> userId1 = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.getByUserIds(userId1, null);
        List<VideoVo> vos = new ArrayList<>();
        for (Video item : items) {
            UserInfo userInfo = map.get(item.getUserId());
            if (userInfo != null) {
                VideoVo init = VideoVo.init(userInfo, item);
                vos.add(init);
            }
        }
        pageResult.setItems(vos);
        return pageResult;
    }

    public PageResult getMessages(Integer page, Integer pagesize, Long userId, String state) {
        PageResult pageResult = movementApi.getMovementsState(page, pagesize, userId, state);
        List<Movement> items = (List<Movement>) pageResult.getItems();
        if (CollUtil.isEmpty(items)) {
            return pageResult;
        }
        List<Long> userId1 = CollUtil.getFieldValues(items, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.getByUserIds(userId1, null);
        List<MovementsVo> list = new ArrayList<>();
        for (Movement item : items) {
            UserInfo userInfo = map.get(item.getUserId());
            if (userInfo != null) {
                MovementsVo init = MovementsVo.init(userInfo, item);
                list.add(init);
            }

        }
        pageResult.setItems(list);
        return pageResult;
    }

    public MovementsVo messagesParticulars(ObjectId id) {
        Movement m = movementApi.getById(id);
        if (m == null) {
            return new MovementsVo();
        }
        Long userId = m.getUserId();
        UserInfo userById = userInfoApi.getUserById(userId);
        MovementsVo init = MovementsVo.init(userById, m);
        return init;
    }

    public PageResult getComments(Integer page, Integer pagesize, ObjectId messageID) {
        List<Comment> comments = commentApi.getComments(page, pagesize, messageID);
        if (CollUtil.isEmpty(comments)) {
            return new PageResult();
        }

        List<Long> userId = CollUtil.getFieldValues(comments, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.getByUserIds(userId, null);
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : comments) {
            UserInfo userInfo = map.get(comment.getUserId());
            if (userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }

        return new PageResult(page, pagesize, 0L, vos);
    }

    public UserInfo getUserById(Long id) {
        UserInfo userById = userInfoApi.getUserById(id);
        String key = Constants.USER_FREEZE + id;
        if (redisTemplate.hasKey(key)) {
            userById.setUserStatus("2");
        }
        return userById;

    }

    public Map saveFreeze(Map map) {
        Integer userId = (Integer) map.get("userId");
        Integer freezingTime = Integer.parseInt(map.get("freezingTime").toString());
        Integer freezingRange = Integer.parseInt(map.get("freezingRange").toString()) ;
        String key = Constants.USER_FREEZE + userId;
        int day = 0;
        if (freezingTime == 1) {
            day = 3;
        } else if (freezingTime == 2) {
            day = 7;
        }
        String s = JSON.toJSONString(map);
        if (day > 0) {
            redisTemplate.opsForValue().set(key, s, day, TimeUnit.DAYS);
        }else{
            redisTemplate.opsForValue().set(key, s);
        }
        Map map1 = new HashMap();
        map1.put("message", "操作成功");
        return map1;
    }

    public Map deleteFreeze(Map map) {
        Long userId = Long.valueOf((Integer) map.get("userId"));
        String key = Constants.USER_FREEZE + userId;
        redisTemplate.delete(key);
        Map map1 = new HashMap();
        map1.put("message", "操作成功");
        return map1;
    }
}
